﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Agility.ViewModels;
using FirstLook.ServiceModel.DomainServices.Client.Security;
using Agility.Web.Models.DomainModel;

namespace Agility.Controls
{
    public partial class MyIdeas : UserControl
    {
        public MyIdeas()
        {
            InitializeComponent();
            this.DataContext = new MyIdeasViewModel();
        }

        private void dataGridMyIdeas_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Title":
                    e.Column.MaxWidth = 200;
                    break;
                case "Description":
                    e.Column.MaxWidth = 500;
                    break;
                case "EffortSize":
                case "PotentialBusinessValue":
                case "StrategyAlignment":
                case "RankingValue":
                    if (!WebContext.Current.User.IsInRole("Business Analyst") || !WebContext.Current.User.IsInRole("Product Manager"))
                        e.Column.Visibility = System.Windows.Visibility.Collapsed;
                    break;
                case "IsApproved":
                    if (WebContext.Current.User.IsInRole("Registered User") || WebContext.Current.User.IsInRole("Business Analyst"))
                        e.Column.IsReadOnly = true;
                    break;
                default:
                    break;
            }
        }

        private void dataGridMyIdeas_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dataFormIdea.Mode != DataFormMode.ReadOnly)
                dataFormIdea.CancelEdit();
        }

        private void dataFormIdea_EditEnded(object sender, DataFormEditEndedEventArgs e)
        {
            if (e.EditAction == DataFormEditAction.Commit)
                ((MyIdeasViewModel)this.DataContext).SubmitChanges();
        }

        private void dataFormIdea_ContentLoaded(object sender, DataFormContentLoadEventArgs e)
        {
            if (e.Mode == DataFormMode.AddNew)
                ((Epic)e.Content.DataContext).RequestedBy = WebContext.Current.User.Name;
        }
    }
}
